﻿using System;
using System.Collections;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
using ChapterX.Common;
using ChapterX.Data;
using ChapterX.Data.CoursesDSTableAdapters;


namespace ChapterX.BusinessFacade
{
    public class CourseSystem
    {
        #region Properties

        private CoursesDS.CoursesDataTable _courseSchema = null;
        public CoursesDS.CoursesDataTable CourseSchema
        {
            get
            {
                if (null == _courseSchema)
                {
                    _courseSchema = new CoursesDS.CoursesDataTable();
                }
                return _courseSchema;
            }
        }

        private CoursesDS.CourseBookDataTable _coursebookSchema = null;
        public CoursesDS.CourseBookDataTable CourseBookSchema
        {
            get 
            {
                if (null == _coursebookSchema)
                {
                    _coursebookSchema = new CoursesDS.CourseBookDataTable();
                }
                return _coursebookSchema;
            }
        }

        private CoursesDS.CourseAttachmentDataTable _courseAttachmentSchema = null;
        public CoursesDS.CourseAttachmentDataTable CourseAttachmentSchema
        {
            get
            {
                if (null == _courseAttachmentSchema)
                {
                    _courseAttachmentSchema = new CoursesDS.CourseAttachmentDataTable();
                }
                return _courseAttachmentSchema;
            }
        }

        //private Courses.DetailCoursesViewDataTable _courseSchema = null;

        //public Courses.DetailCoursesViewDataTable CourseSchema
        //{
        //    get
        //    {
        //        if (null == _courseSchema)
        //        {
        //            _courseSchema = new Courses.DetailCoursesViewDataTable();
        //        }
        //        return _courseSchema;
        //    }
        //}

        //private Courses.DetailCoursesTextbooksViewDataTable _textbookSchema = null;

        //public Courses.DetailCoursesTextbooksViewDataTable TextbookSchema
        //{
        //    get
        //    {
        //        if (null == _textbookSchema)
        //        {
        //            _textbookSchema = new Courses.DetailCoursesTextbooksViewDataTable();
        //        }
        //        return _textbookSchema;
        //    }
        //}

        //private Hashtable _columnNameColumnMap = null;

        //protected Hashtable ColumnNameColumnMap
        //{
        //    get
        //    {
        //        if (null == _columnNameColumnMap)
        //        {
        //            _columnNameColumnMap = new Hashtable();
        //            using (CoursesColumnsTableAdapter adapter = new CoursesColumnsTableAdapter())
        //            {
        //                Courses.CoursesColumnsDataTable columnList = adapter.GetData();
        //                foreach (Courses.CoursesColumnsRow column in columnList)
        //                {
        //                    _columnNameColumnMap.Add(column.ColumnName, column);
        //                }
        //            }
        //        }
        //        return _columnNameColumnMap;
        //    }
        //}

        //private Hashtable _additionalColumnNameMap = null;

        //protected Hashtable AdditionalColumnNameMap
        //{
        //    get
        //    {
        //        if (null == _additionalColumnNameMap)
        //        {
        //            _additionalColumnNameMap = new Hashtable();
        //            _additionalColumnNameMap.Add("NO", null);
        //            _additionalColumnNameMap.Add("BranchCode", null);
        //            _additionalColumnNameMap.Add("BranchName", null);
        //            _additionalColumnNameMap.Add("IsPrized", null);
        //            _additionalColumnNameMap.Add("IsValid", null);
        //            _additionalColumnNameMap.Add("IsChecked", null);
        //            _additionalColumnNameMap.Add("type", null);
        //            _additionalColumnNameMap.Add("Introduction", CourseSchema.MajorIntroColumn.ColumnName);
        //            _additionalColumnNameMap.Add("InsertDate", CourseSchema.SubmitTimeColumn.ColumnName);
        //        }
        //        return _additionalColumnNameMap;
        //    }
        //}

        //private Hashtable _defaultColumnValues = null;

        //protected Hashtable DefaultColumnValues
        //{
        //    get
        //    {
        //        if (null == _defaultColumnValues)
        //        {
        //            _defaultColumnValues = new Hashtable();
        //        }
        //        return _defaultColumnValues;
        //    }
        //}

        #endregion

        public CoursesDS.CoursesRow GetCourseByCourseId(int courseId)
        {
            CoursesTableAdapter adapter = new CoursesTableAdapter();
            CoursesDS.CoursesDataTable table = adapter.GetCourseByCourseId(courseId);

            if (null != table && table.Rows.Count > 0)
            {
                return table[0];
            }
            else
            {
                return null;
            }
        }

        public bool SubmitCourse(CoursesDS.CoursesRow course, String submitUser)
        {
            bool result = false;

            try
            {
                if (Verify(course))
                {
                    CoursesTableAdapter adapter = new CoursesTableAdapter();

                    if (course.CourseId < 0)
                    {
                        // 课程信息不存在，插入课程信息
                        result = InsertCourse(adapter, course, submitUser);
                    }
                    else
                    {
                        // 课程信息已存在，更新课程信息
                        result = UpdateCourse(adapter, course, submitUser);
                    }
                }
            }
            catch (VerifyException ex)
            {
                throw ex;
            }
            catch (UnauthorizedException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new GenericException(ex.Message);
            }

            return result;
        }

        protected bool InsertCourse(CoursesTableAdapter adapter, CoursesDS.CoursesRow course, String submitUser)
        {
            bool result = false;

            try
            {
                int? courseId = null;
                adapter.InsertCourse(ref courseId,
                    course.CourseNumber,
                    course.CourseName, 
                    course.IsDepartmentNull() ? null : course.Department.Trim(),
                    course.IsSemesterNull() ? null : course.Semester,
                    course.IsAudienceNull() ? null : course.Audience.Trim(),
                    course.IsCreditNull() ? (int?)null : course.Credit, 
                    course.IsAssessmentNull() ? null : course.Assessment,
                    course.IsVolumeNull() ? (int?)null : course.Volume,
                    course.TeacherId,
                    submitUser);

                if (courseId.HasValue)
                {
                    course.CourseId = courseId.Value;
                    result = true;
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
                result = false;
            }

            return result;
        }

        protected bool UpdateCourse(CoursesTableAdapter adapter, CoursesDS.CoursesRow course, String updateUser)
        {
            bool result = false;

            try
            {
                result = (adapter.UpdateCourse(course.CourseId,
                    course.CourseNumber,
                    course.CourseName, 
                    course.IsDepartmentNull() ? null : course.Department.Trim(),
                    course.IsSemesterNull() ? null : course.Semester,
                    course.IsAudienceNull() ? null : course.Audience.Trim(),
                    course.IsCreditNull() ? (int?)null : course.Credit, 
                    course.IsAssessmentNull() ? null : course.Assessment,
                    course.IsVolumeNull() ? (int?)null : course.Volume,
                    course.TeacherId,
                    updateUser) == 1);
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
                result = false;
            }

            return result;
        }

        public bool DeleteCourse(int courseId)
        {
            bool result = false;

            try
            {
                CoursesTableAdapter adapter = new CoursesTableAdapter();
                result = (adapter.Delete(courseId) == 1);
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
                result = false;
            }

            return result;
        }

        public bool Verify(CoursesDS.CoursesRow course)
        {
            return true;
        }

        public bool BindCourseBook(int courseId, int bookId, String type)
        {
            bool result = false;

            try
            {
                CourseBookTableAdapter adapter = new CourseBookTableAdapter();
                result = (adapter.InsertCourseBook(courseId, bookId, type, 
                    (int)CommonDefinition.CourseBookStatus.defaultStatus) == 1);
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
                result = false;
            }

            return result;
        }

        public bool UpdateCourseBookStatus(int courseId, int bookId, int status)
        {
            bool result = false;

            try
            {
                CourseBookTableAdapter adapter = new CourseBookTableAdapter();
                result = (adapter.UpdateCourseBookStatus(courseId, bookId, status) == 1);
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
                result = false;
            }

            return result;
        }

        public int GetCourseBookStatus(int courseId, int bookId)
        {
            CourseBookTableAdapter adapter = new CourseBookTableAdapter();
            CoursesDS.CourseBookDataTable table = adapter.GetCourseBookByCourseIdAndBookId(courseId, bookId);

            if (null != table && table.Rows.Count > 0)
            {
                return table[0].Status;
            }
            else
            {
                return -1;
            }
        }

        #region Course Attachment

        public bool InsertCourseAttachment(CoursesDS.CourseAttachmentRow attachment)
        {
            bool result = false;
            try
            {
                CourseAttachmentTableAdapter adapter = new CourseAttachmentTableAdapter();

                int r = adapter.InsertCourseAttachment(
                    attachment.CourseId,
                    attachment.Title,
                    attachment.FilePath);

                if (r > 0)
                    result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }

        #endregion
    }
}